﻿using System;
using fastCSharp;

namespace diantou.web.contest
{
    /// <summary>
    /// 比赛得分排行榜
    /// </summary>
    [fastCSharp.code.cSharp.webView(IsPool = true, IsReferer = false)]
    partial class problemRank : problemView<problemRank>
    {
        /// <summary>
        /// 比赛得分排行榜缓存数据
        /// </summary>
        private struct cacheValue
        {
            /// <summary>
            /// 比赛得分排行数据
            /// </summary>
            public diantou.dataAccess.contestJudge.problemRankItem[] Items;
            /// <summary>
            /// 总数量
            /// </summary>
            public int Count;
        }
        /// <summary>
        /// 比赛得分排行榜客户端缓存
        /// </summary>
        private static readonly fastCSharp.sql.logCache<int, cacheValue>.array cache = new fastCSharp.sql.logCache<int, cacheValue>.array(diantou.dataAccess.tcpCall.contestJudge.onProblemRankLog, 8);
        /// <summary>
        /// 
        /// </summary>
        [fastCSharp.emit.webView.clearMember]
        private pointRank.rank[] ranks;
        /// <summary>
        /// 当前用户排行
        /// </summary>
        private pointRank.rank topUser;
        /// <summary>
        /// 当前用户排行
        /// </summary>
        private pointRank.rank bottomUser;
        /// <summary>
        /// 比赛得分排行榜
        /// </summary>
        /// <param name="problemId"></param>
        /// <param name="page">分页号</param>
        /// <returns>是否成功</returns>
        private bool loadView(int problemId, int page)
        {
            if (base.loadView(problemId))
            {
                fastCSharp.sql.logCache<int, cacheValue>.array.value cacheValue = page <= 1 ? cache.Get(problemId) : default(fastCSharp.sql.logCache<int, cacheValue>.array.value);
                if (cacheValue.Value.Items == null)
                {
                    cacheValue.Value.Items = diantou.dataAccess.tcpCall.contestJudge.getProblemRank(problemId, 20, page, out cacheValue.Value.Count);
                    cache.Set(ref cacheValue);
                }
                pageInfo.Page = page;
                pageInfo.Set(20, cacheValue.Value.Count);
                array.page arrayPage = new array.page(pageInfo.Count, pageInfo.Size, page);
                int rank = arrayPage.SkipCount, userId = currentUserId, index = 0, isUser = 0;
                ranks = new pointRank.rank[arrayPage.CurrentPageSize];
                foreach (diantou.dataAccess.contestJudge.problemRankItem user in cacheValue.Value.Items)
                {
                    if (user.User.Id == userId)
                    {
                        ranks[index++].Set(user, ++rank, true);
                        isUser = 1;
                    }
                    else ranks[index++].Set(user, ++rank, false);
                }
                if (userId != 0 && isUser == 0)
                {
                    diantou.dataAccess.contestJudge.problemRankItem item = diantou.dataAccess.tcpCall.contestJudge.getProblemRank(problemId, userId, out index);
                    if (index >= 0)
                    {
                        if (index > rank) bottomUser.Set(item, index, true);
                        else topUser.Set(item, index, true);
                    }
                }
                if (requestHeader.IsSearchEngine) pageInfo.Path = problem.Path.ContestRank.ToPageString();
                return true;
            }
            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void clear()
        {
            base.clear();
            topUser.User = bottomUser.User = null;
        }

    }
}
